<!DOCTYPE html>
<html lang="zh-cn">

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>注册</title>
  <!-- 导入jquery -->
  <script src="./libs/jquery-1.12.4.min.js"></script>
  <!-- 导入bootstrap -->
  <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css">
  <script src="./libs/bootstrap/js/bootstrap.min.js"></script>
  <!-- 导入art-template -->
  <script src="./libs/template-web.js"></script>
  <!-- 导入md5 -->
  <script src="./libs/md5.min.js"></script>
  <style>
    /* 把登录框往下压 */
    .my-panel {
      margin-top: 200px;
    }
  </style>
</head>

<body>
  <!-- bootstrap的容器  带版心的-->
  <div class="container">
    <!-- 栅格的行 -->
    <div class="row">
      <!-- 栅格的列 offset 列偏移 -->
      <div class="col-xs-6 col-xs-offset-3">
        <!-- 面板 -->
        <div class="panel panel-default panel-primary my-panel">
          <!-- 面板的标题 -->
          <div class="panel-heading">
            <h3 class="panel-title">登录CQ管理系统</h3>
          </div>
          <!-- 面板的内容 -->
          <div class="panel-body">
            <!-- 表单 -->
            <form class="form-horizontal" id="form">
              <div class="form-group">
                <label for="userName" class="col-sm-2 control-label">用户名</label>
                <div class="col-sm-10">
                  <input type="text" name="username" class="form-control" id="username" placeholder="王**">
                </div>
              </div>
              <div class="form-group">
                <label for="passWord" class="col-sm-2 control-label">密&nbsp;&nbsp;&nbsp;&nbsp;码</label>
                <div class="col-sm-10">
                  <input type="password" name="password" class="form-control" id="password" placeholder="密码">
                </div>
              </div>
              <div class="form-group">
                <label for="code" class="col-sm-2 control-label">验证码</label>
                <div class="col-sm-6">
                  <input type="text" name="code" class="form-control" id="code" placeholder="验证码">
                </div>
                <div class="col-sm-4">
                  <img class="code" src="http://127.0.0.1:8089/captcha?" alt="">
                </div>
              </div>
              <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                  <input type="submit" class="btn btn-danger" id="register" value="注册">
                </div>
              </div>
            </form>
          </div>
        </div>
      </div>
    </div>
  </div>
</body>

</html>
<script>
  $(function () {
    $(".code").on("click", function () {
      $(this).attr("src", "http://127.0.0.1:8089/captcha?sb=" + Math.random() * 999999)
    })
    $("#register").on("click", function (e) {
      e.preventDefault()
      $("#password").val(md5($("#password").val().trim(), 'jingjing'))

      let username = $("#username").val().trim()
      let password = $("#password").val().trim()
      let code = $("#code").val().trim()

      console.log(username, password, code);

      $.ajax({
        type: 'post',
        url: "http://127.0.0.1:8089/hero/register",
        data: {
          username,
          password,
          code
        },
        success: function (backData) {
          if (backData.code == 201) {
            alert(backData.msg)
            window.location.href = "./login.html"
          } else {
            alert(backData.msg)
          }
        }

      })
    })

    // $("#register").on("click", function (e) {
    //   e.preventDefault()
    //   $("#password").val(md5($("#password").val().trim(), 'jingjing'))
    //   let username = $("#username").val().trim()
    //   let password = $("#password").val().trim()
    //   let code = $("#code").val().trim()
    //   console.log(username, password, code);
    //   $.ajax({
    //     type: 'post',
    //     url: "http://127.0.0.1:8089/hero/register",
    //     data: {
    //       username,
    //       password,
    //       code
    //     },
    //     success: function (backData) {
    //       console.log(backData);
    //     }
    //   })
    // })

  })

  // $(function () {
  //   $(".code").on("click", function () {
  //     console.log('图片点击了');

  //     $(this).attr("src", 'http://127.0.0.1:8089/captcha?sb=' + Math.random() * 9483)
  //   })

  //   $("#register").on("click", function (e) {
  //     e.preventDefault()
  //     $("#password").val(md5($("#password").val().trim(), 'jingjing'))
  //     let username = $("#username").val().trim()
  //     let password = $("#password").val().trim()
  //     let code = $("#code").val().trim()


  //     if (username == '' && password == '' && code == '') {
  //       return
  //     }
  //     console.log(username, password, code);
  //     $.ajax({
  //       type: 'post',
  //       url: "http://127.0.0.1:8089/hero/register",
  //       data: {
  //         username,
  //         password,
  //         code
  //       },
  //       success: function (backData) {
  //         console.log(backData);
  //       }
  //     })
  //   })
  // })
</script>